package com.bs.visitservice.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bs.visitservice.entity.Seller;
import com.bs.visitservice.entity.SellerPhotos;
import com.bs.visitservice.service.SellerPhotosService;
import com.bs.visitservice.service.SellerService;
import com.bs.visitservice.utils.ParamUtils;
import com.bs.visitservice.utils.Result;
import com.bs.visitservice.utils.ResultUtil;
import io.netty.util.internal.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 小程序首页商家展示
 */
@RestController
@RequestMapping("/userpublic")
public class SellerListController {
    @Autowired
    private SellerService sellerService;

    @Autowired
    private SellerPhotosService sellerPhotosService;
    /**
     * 分页获取所有商家信息,1正常状态,3休息中
     * @return
     */
    @RequestMapping(value = "/getAllSeller",method = RequestMethod.POST)
    public Result getSellerList(@RequestBody Map cond){
        Seller s = (Seller)ResultUtil.transObject(cond,Seller.class);
        IPage page = ParamUtils.getPage(cond);
        LambdaQueryWrapper<Seller> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Seller::getStatus,1).or().eq(Seller::getStatus,3);
        if(!StringUtils.isEmpty(s.getSellerName())){
            wrapper.like(Seller::getSellerName,s.getSellerName());
        }
        if(!StringUtils.isEmpty(s.getServeType())){
            wrapper.eq(Seller::getServeType,s.getServeType());
        }
        if(!StringUtils.isEmpty(s.getProvince())){
            wrapper.eq(Seller::getProvince,s.getProvince());
        }
        if(!StringUtils.isEmpty(s.getCity())){
            wrapper.eq(Seller::getCity,s.getCity());
        }
        if(!StringUtils.isEmpty(s.getArea())) {
            wrapper.eq(Seller::getArea, s.getArea());
        }
        wrapper.orderByDesc(Seller::getCreditPoint);
        page  = sellerService.pageMaps(page,wrapper);
        List<Map<String, Object>> res  = page.getRecords();
        ResultUtil.transfort(res,getDictMap(),true);
        Map<String,Object> result = new HashMap<>();
        result.put("total",page.getTotal());
        result.put("datas",res);
        return Result.success(result);
    }


    /**
     * 获取某个商家照片信息
     * @param cond
     * @return
     */
    @RequestMapping(value = "/getPhotoBy",method = RequestMethod.POST)
    public Result getSellerListBy(@RequestBody Map cond){
        Long sellerNo = Long.valueOf((String.valueOf(cond.get("sellerNo"))));
        Integer photoType = (Integer)cond.get("photoType");
        IPage page = ParamUtils.getPage(cond);
        LambdaQueryWrapper<SellerPhotos> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SellerPhotos::getSellerNo, sellerNo).eq(SellerPhotos::getPhotoType,photoType).orderByAsc(SellerPhotos::getSn);
        page = sellerPhotosService.pageMaps(page, wrapper);
        List<Map<String, Object>> res = page.getRecords();
        ResultUtil.transfort(res, getDictMap(), true);
        Map<String,Object> result = new HashMap<>();
        result.put("total",page.getTotal());
        result.put("datas",res);
        return Result.success(result);
    }

    /**
     * 获取列和字典值的映射关系
     * @return
     */
    private Map getDictMap(){
        Map<String,String> dictMap = new HashMap<>();
        dictMap.put("status","sellerStatus");
        dictMap.put("photoType","photoType");
        dictMap.put("serveType","serveType");
        return dictMap;
    }
}
